﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq; 
using System.Net;
using Microsoft.Extensions.Logging;
using System.Extensions;
using System.Threading.Tasks;
using System.Net.Http;

namespace Wfxin.SystemFramework.Attributes
{
    public class TikuAttribute : System.Attribute, IActionFilter
    {
        static readonly ILogger _Logger = LogManager.Create<TikuAttribute>();

        public void OnActionExecuting(ActionExecutingContext context)//方法执行前执行
        {
            string path = context.HttpContext.Request.Path.Value; 
            var requests = context.ActionArguments.Select(item=>new { item.Key,item.Value});

            string result = context.HttpContext.Request.Params();
            if (requests != null)
            {
                _Logger.LogInformation("Path:{0}\r\nRequest:\r\n{1}", path, requests.ToJson());
            } 
            
             
            #region 方法中带参数时 
            //var reqParam = context.ActionArguments.FirstOrDefault(item => item.Value);
            //if (reqParam.Value == null)
            //{

            //    _Logger.LogError(Res.ToJson());
            //    context.Result = ActionResult.CreateResult(HttpStatusCode.BadRequest, Res.ToJson());
            //    return;
            //}
            #endregion
        }

        public void OnActionExecuted(ActionExecutedContext context)//方法执行后执行
        {
            string result = string.Empty;
            try
            {
                result = context.Result.GetPropertyValue("Value").ToJson();
            } catch (Exception) { }
            _Logger.LogInformation("Path:{0}\r\nResponse:\r\n{1}", context.HttpContext.Request.Path.Value, result);
        }

    }
}
